<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create collection index</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mysql-xdevapi-collection.count.html">« Collection::count</a></li>
      <li style="float: right;"><a href="mysql-xdevapi-collection.dropindex.html">Collection::dropIndex »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mysql-xdevapi-collection.html">mysql_xdevapi\Collection</a></li>
    <li>Create collection index</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mysql-xdevapi-collection.createindex" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Collection::createIndex</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">Collection::createIndex</span> &mdash; <span class="dc-title">Create collection index</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mysql-xdevapi-collection.createindex-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>mysql_xdevapi\Collection::createIndex</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$index_name</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$index_desc_json</code></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
    Creates an index on the collection.
  </p>
  <p class="para">
    An exception is thrown if an index with the same name already exists,
    or if index definition is not correctly formed.
   </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mysql-xdevapi-collection.createindex-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">index_name</code></dt>

    <dd>

     <p class="para">
      The name of the index that to create. This name must be a valid index name as 
      accepted by the <code class="literal">CREATE INDEX</code> SQL query.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">index_desc_json</code></dt>

    <dd>

     <p class="para">
      Definition of the index to create. It contains an array of IndexField objects,
      and each object describes a single document member to include in the index, 
      and an optional string for the type of index that might be INDEX (default) or SPATIAL.
     </p>
     <p class="para">
      A single IndexField description consists of the following fields:
     </p>
     <ul class="itemizedlist">
      <li class="listitem">
        <p class="para">
          <code class="code">field</code>: string, the full document path to the document member or field to be indexed.
        </p>
      </li>
      <li class="listitem">
       <p class="para">
        <code class="code">type</code>: string, one of the supported SQL column types to map the field into.
        For numeric types, the optional UNSIGNED keyword may follow.
        For the TEXT type, the length to consider for indexing may be added.
       </p>
      </li>
      <li class="listitem">
        <p class="para">
          <code class="code">required</code>: bool, (optional) true if the field is required to exist in the document. 
          Defaults to <strong><code>false</code></strong>, except for <code class="literal">GEOJSON</code> where it defaults to <strong><code>true</code></strong>.
         </p>
       </li>
      <li class="listitem">
        <p class="para">
          <code class="code">options</code>: integer, (optional) special option flags for use 
          when decoding <code class="literal">GEOJSON</code> data.
        </p>
      </li>
      <li class="listitem">
        <p class="para">
          <code class="code">srid</code>: integer, (optional) srid value for use when 
          decoding <code class="literal">GEOJSON</code> data.
        </p>
      </li>
      </ul>

      <p class="para">
        It is an error to include other fields not described above in 
        IndexDefinition or IndexField documents.
      </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mysql-xdevapi-collection.createindex-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
 </div>


 <div class="refsect1 examples" id="refsect1-mysql-xdevapi-collection.createindex-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1560">
   <p><strong>示例 #1 <span class="function"><strong>mysql_xdevapi\Collection::createIndex()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$session&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_xdevapi</span><span style="color: #007700">\</span><span style="color: #0000BB">getSession</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysqlx://user:password@localhost"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sql</span><span style="color: #007700">(</span><span style="color: #DD0000">"DROP&nbsp;DATABASE&nbsp;IF&nbsp;EXISTS&nbsp;addressbook"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sql</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE&nbsp;DATABASE&nbsp;addressbook"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$schema&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSchema</span><span style="color: #007700">(</span><span style="color: #DD0000">"addressbook"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$schema</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"people"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Creating&nbsp;a&nbsp;text&nbsp;index<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createIndex</span><span style="color: #007700">(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'myindex1'</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'{"fields":&nbsp;[{<br />&nbsp;&nbsp;&nbsp;&nbsp;"field":&nbsp;"$.name",&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;"type":&nbsp;"TEXT(25)",&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;"required":&nbsp;true}],&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;"unique":&nbsp;false}'<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;spatial&nbsp;index<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createIndex</span><span style="color: #007700">(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'myindex2'</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'{"fields":&nbsp;[{<br />&nbsp;&nbsp;&nbsp;&nbsp;"field":&nbsp;"$.home",&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;"type":&nbsp;"GEOJSON",&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;"required":&nbsp;true}],&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;"type":&nbsp;"SPATIAL"}'<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Index&nbsp;with&nbsp;multiple&nbsp;fields<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createIndex</span><span style="color: #007700">(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'myindex3'</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'{"fields":&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"field":&nbsp;"$.name",<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type":&nbsp;"TEXT(20)",<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"required":&nbsp;true<br />&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"field":&nbsp;"$.age",<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type":&nbsp;"INTEGER"<br />&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"field":&nbsp;"$.job",<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type":&nbsp;"TEXT(30)",<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"required":&nbsp;false<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;],<br />&nbsp;&nbsp;"unique":&nbsp;true<br />&nbsp;&nbsp;}'<br /></span><span style="color: #007700">);</span>
</span>
</code></div>
   </div>

  </div>
 </div>


</div></div></div></body></html>